Fix VesselDistanceComparer throwing ArgumentException when vessel position is NaN#388
Open
kilfoil wants to merge 1 commit into
Open
Fix VesselDistanceComparer throwing ArgumentException when vessel position is NaN#388kilfoil wants to merge 1 commit into
kilfoil wants to merge 1 commit into
Conversation
…ition is NaN Array.Sort() requires a consistent IComparer. The original implementation casts float subtraction to int: (int)(distA - distB). This produces inconsistent results when any vessel's transform position contains NaN (NaN - NaN = NaN; (int)NaN = 0 regardless of operand order), which violates the sort contract and causes an ArgumentException from Array.Sort() on every FixedUpdate. Also fixes a secondary issue where very large squared distances (> int.MaxValue) could overflow and produce incorrect ordering. Fix: replace (int)(distA - distB) with distA.CompareTo(distB), which handles NaN and large values correctly.
70a098b to
0c1d710
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
MASFlightComputerProxy.VesselDistanceComparer.Compare()uses:Array.Sort()requires a consistentIComparer. If any vessel'sGetTransform().positioncontainsNaN(which can occur with certain save states), thendistAordistBisNaN, and(int)(NaN - NaN)evaluates to0regardless of operand order. This violates the sort contract (transitivity) and causesArray.Sort()to throw:This fires on every
FixedUpdate(50×/sec) while in IVA with nearby vessels, generating thousands of log entries and breaking any MFD page that displays neighbouring vessel information.The same
(int)cast also overflows silently for very large squared distances (>int.MaxValue≈ 46,340 km), producing incorrect sort order at interplanetary distances.Fix
Replace the cast with
float.CompareTo(), which handlesNaNand large values correctly:One character change; no behaviour change for normal finite distances.